package com.blu.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.jsoup.Jsoup;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.batch.JobLauncherApplicationRunner;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.blu.job.JobCount;
import com.blu.job.invalidlist;
import com.blu.job.parselist;

import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
public class Tcontroller {
	
	@Autowired
	private Scheduler scheduler;
	
	@GetMapping("start")
	public String start() throws Exception {
		if(scheduler.isStarted()) {
			return "任务已经启动,启动时间："+ JobCount.createTime;
		} else {
			scheduler.start();
			JobCount.createTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());;
			return "刷浏览量程序已成功启动......";
		}
		
	}
	
	@GetMapping("count")
	public String count() throws Exception {
		return "任务已执行次数:"+ JobCount.count+"  链接数量："+JobCount.list.size() +" 任务启动时间："+JobCount.createTime;
	}
	
	@PostMapping("add")
	public String add(@RequestParam String url) throws SchedulerException, InterruptedException {
		if(JobCount.list.contains(url)) {
			return "链接已经存在";
		} else if(JobCount.list.size()>=100) {
			return "当前添加的链接数已达100个上限";
		} else {
			try{
				Jsoup.connect(url).userAgent("Mozilla").get();
				JobCount.list.add(url);
				log.info("新增链接："+url);
				return "链接添加成功";
			}catch (Exception e) {
				log.info("添加的链接无效："+url);
				return "无效链接";
			}
		}
		
	}
	
	@PostMapping("remove")
	public String remove(@RequestParam String url) throws InterruptedException, SchedulerException {
		if(JobCount.list.contains(url)) {
			JobCount.list.remove(url);
			log.info("删除链接："+url);
			return "链接删除成功";
		} else {
			return "链接不存在";
		}
		
	}
	
	@GetMapping("delete")
	public String delete(@RequestParam String url) {
		if(JobCount.list.contains(url)) {
			JobCount.list.remove(url);
			System.out.println("删除链接："+url);
			return "链接删除成功";
		} else {
			return "链接不存在";
		}
	}
	
	@GetMapping("deleteinvalid")
	public String deleteinvalid(@RequestParam String url) {
		if(invalidlist.list.contains(url)) {
			invalidlist.list.remove(url);
			System.out.println("删除链接："+url);
			return "链接删除成功";
		} else {
			return "链接不存在";
		}
	}
	
	@GetMapping("addall")
	public String addAll() {
		for(String url : parselist.list) {
			if(!JobCount.list.contains(url)) {
				if(!(JobCount.list.size()>=100)) {
					JobCount.list.add(url);
				} else {
					return "已添加至100个上限";
				}
			}
		}
		return "全部添加成功";
	}
	
	@GetMapping("removeall")
	public String removeAll() {
		JobCount.list.clear();
		return "全部删除成功";
	}
	
	@GetMapping("parseadd")
	public String parseadd(@RequestParam String url) throws SchedulerException, InterruptedException {
		if(JobCount.list.contains(url)) {
			return "链接已经存在";
		} else if(JobCount.list.size()>=100) {
			return "当前添加的链接数已达100个上限";
		} else {
			try{
				Jsoup.connect(url).userAgent("Mozilla").get();
				JobCount.list.add(url);
				log.info("新增链接："+url);
				return "链接添加成功";
			}catch (Exception e) {
				log.info("添加的链接无效："+url);
				return "无效链接";
			}
		}
		
	}
	
}
